package Gather.GatherExercise;

import java.util.ArrayList;

/**
 * ClassName:PhoneText001
 * Package:Gather.GatherExercise
 * Description:
 *
 * @Author:ZhangXin
 * @Create2023/1/18-1:26
 * @Version:v1.0
 */
public class PhoneText001 {
    public static void main(String[] args) {
        ArrayList<Phone> list = new ArrayList<>();
        Phone p1 = new Phone("小米", 1000);
        Phone p2 = new Phone("苹果", 8000);
        Phone p3 = new Phone("锤子", 2999);

        list.add(p1);
        list.add(p2);
        list.add(p3);

        //调用方法
        ArrayList<Phone> phoneInfoList = getPhoneInfo(list);
        //遍历集合
        for (int i = 0; i < phoneInfoList.size(); i++) {
            Phone phone = phoneInfoList.get(i);
            System.out.println(phone.getBrand()+","+phone.getPrice());
        }
    }

    //技巧
    //如果我们要返回多个数据,可以把这些数据先放到一个容器当中,再把容器返回
    //集合  数组
    public static ArrayList<Phone> getPhoneInfo(ArrayList<Phone> list) {
        //定义一个集合用于存储价格低于3000的手机对象
        ArrayList<Phone> resultList = new ArrayList<>();
        //遍历集合
        for (int i = 0; i < list.size(); i++) {
            Phone p = list.get(i);
            double price = p.getPrice();
            //如果当前手机的价格低于3000,那么就把手机对象添加到resultList中
            if (price < 3000) {
                resultList.add(p);
            }
        }
        //返回resultList
        return resultList;
    }

}
